package org.example.common;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

import java.io.InputStream;

public class AliOssUtil {
    private static final String ENDPOINT = "https://oss-cn-beijing.aliyuncs.com";
    private static final String ACCESS_KEY_ID="LTAI5tHpkZjnSiXHivwoz4sB";
    private static final String SECRET_ACCESS_KEY="IheyQZoABBnId6dYz777pTg5e9ZuXt";
    private static final String BUCKET_NAME="wnagshuo";
    public static String uploadFile(String objectName, InputStream inputStream){
        OSS oss=new OSSClientBuilder().build(ENDPOINT,ACCESS_KEY_ID,SECRET_ACCESS_KEY);
        String url = "";
        try{
            oss.createBucket(BUCKET_NAME);
            oss.putObject(BUCKET_NAME,objectName,inputStream);
            url= "https://"+BUCKET_NAME+"."+ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1)+"/"+objectName;

        }catch (OSSException oe){
            System.out.println("请求错误"+oe.getMessage());

        }catch (ClientException cl){
            System.out.println("cl"+cl.getMessage());

        }finally {
            if(oss!=null){
                oss.shutdown();
            }
        }
        return  url;
    }
}
